<template>
  <PageWrapper>
    <a-card :bordered="false" class="mb-3 proCard">
      <a-space align="center">
        <a-input-group style="width: 380px">
          <a-input
            :style="{ width: '100%' }"
            v-model="params.keyword"
            allow-clear
            placeholder="请输入用户名称"
            @keyup.enter="reloadTable"
          />
          <a-button type="primary" @click="reloadTable">
            <template #icon>
              <a-icon>
                <SearchOutlined />
              </a-icon>
            </template>
            查询
          </a-button>
        </a-input-group>
      </a-space>
    </a-card>
    <a-card :bordered="false" class="proCard">
      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        row-key="userId"
        ref="basicTableRef"
        :actionColumn="actionColumn"
        virtual-scroll
      />
    </a-card>
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { h, reactive, ref } from 'vue';
  import { Message } from '@arco-design/web-vue';
  import { BasicTable, TableAction, BasicColumn } from '@/components/Table';
  import { onlineList, onlineLogOut } from '@/api/system/user';
  import { SearchOutlined } from '@vicons/antd';
  import { columns } from './columns';

  const basicTableRef = ref();
  const tableData = ref();

  const params = reactive({
    keyword: '',
  });

  const actionColumn: BasicColumn = reactive({
    width: 100,
    title: '操作',
    dataIndex: 'action',
    fixed: 'right',
    align: 'center',
    render({ record }) {
      return h(TableAction as any, {
        actions: [
          {
            label: '强退',
            type: 'primary',
            status: 'danger',
            popConfirm: {
              content: '您确定要强退吗？',
              okText: '确定',
              cancelText: '取消',
              onOk: handleLogOut.bind(null, record),
            },
          },
        ],
      });
    },
  });

  const loadDataTable = async (res) => {
    const result = await onlineList({ ...params, ...res });
    tableData.value = result.list;
    return result;
  };

  function reloadTable() {
    basicTableRef.value.reload();
  }

  function handleLogOut(row) {
    onlineLogOut({ userId: row.userId }).then(() => {
      Message.success('强退成功');
      reloadTable();
    });
  }
</script>

<style lang="less" scoped></style>
